﻿using DataWasher.WebApi.Common;
using DataWasher.WebApi.IServices;
using Microsoft.Azure.Cosmos.Table;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ToolManager.Entities;
using ToolManager.IService;

namespace DataWasher.WebApi.Services
{
    public class CalculateService : ICalculateService
    {
        private readonly IStorageService _storageService;
        public CalculateService(IStorageService storageService)
        {
            _storageService = storageService;
        }

        public async Task<bool> CalculateSQX()
        {

            //var tableBatchOperation = new TableBatchOperation();
            //var monthDataCount = 0;
            //var preMonthDataCount = 0;
            //SQXEntity sqxEntity = null;

            //var thisYear = HelpTool.GetMonthsThisYear();
            //var filter = await ParamBuilder();
            //try
            //{
            //    var query = new TableQuery<BusinessDataEntity>().Where(filter);
            //    var queryData = await _storageService.GetBusData(query);
            //    var thisYearData = queryData.Where(x => x.AddDate != string.Empty && DateTime.Parse(x.AddDate).Year == thisYear.Year).ToList();
            //    var preYearData = queryData.Where(x => x.AddDate != string.Empty && DateTime.Parse(x.AddDate).Year == thisYear.Year - 1).ToList();
            //    foreach (var month in thisYear.Months)
            //    {
            //        sqxEntity = new SQXEntity(DateTime.Now.ToString("yyyyMMdd"));
            //        monthDataCount = thisYearData.GroupBy(x => x.StoreName).Select(g => new { StoreName = g. }).Where(x => DateTime.Parse(x.AddDate).Month == month)
            //        sqxEntity.Year = thisYear.Year;
            //        sqxEntity.Month = month;
            //        sqxEntity.Count = monthDataCount;
            //        preMonthDataCount = preYearData.Where(x => DateTime.Parse(x.AddDate).Month == month).ToList().Count();
            //        sqxEntity.PreCount = preMonthDataCount;
            //        sqxEntity.YoY = (preMonthDataCount == 0 ? 0 : Math.Round(((decimal)monthDataCount - preMonthDataCount) / preMonthDataCount, 2)).ToString();
            //        sqxEntity.DP = "0";
            //        tableBatchOperation.InsertOrReplace(sqxEntity);
            //    }
            //    await _storageService.InsertBatchData(tableBatchOperation, "SQXItems");
            //    return true;
            //}
            //catch (Exception ex)
            //{
            //    throw ex;
            //}
            return false;
            

        }
        public async Task<string> ParamBuilder()
        {
            var partitionKey = DateTime.Now.ToString("yyyyMMdd");
            var filterSB = new StringBuilder();
            filterSB.Append($"(PartitionKey eq '{partitionKey}')");
            filterSB.Append(" and ");
            filterSB.Append("(MntStatus eq '0')");
            filterSB.Append(" or ");
            filterSB.Append("(MntStatus eq '2')");
            filterSB.Append(" or ");
            filterSB.Append("(MntStatus eq '6')");//待派工--TBD
            filterSB.Append(" or ");
            //filterSB.Append("(MntStatus eq '6')");//待验收 --TBD
            //filterSB.Append(" or ");
            filterSB.Append("(MntStatus eq '8')");
            filterSB.Append(" or ");
            filterSB.Append("(MntStatus eq '10')");
            filterSB.Append(" or ");
            filterSB.Append("(MntStatus eq '7')");

            return filterSB.ToString();
        }
    }
}
